<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | PHP version 5                                                        |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2004 The PHP Group                                |
// +----------------------------------------------------------------------+
// | This source file is subject to version 3.0 of the PHP license,       |
// | that is bundled with this package in the file LICENSE, and is        |
// | available through the world-wide-web at the following url:           |
// | http://www.php.net/license/3_0.txt.                                  |
// | If you did not receive a copy of the PHP license and are unable to   |
// | obtain it through the world-wide-web, please send a note to          |
// | license@php.net so we can mail you a copy immediately.               |
// +----------------------------------------------------------------------+
// | Authors: Original Author <author@example.com>                        |
// |          Your Name <you@example.com>                                 |
// +----------------------------------------------------------------------+
//
// $Id:$

namespace Addons\Auth\Controller;
class IndexController extends \Addon
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 权限列表
     * @return
     */
    public function index()
    {
        $where = 'A.group_id != 0 and G.is_show=1';
        $order = 'id desc';
        $model = BoxModel('addon_auth');
        if ($this->kw = urldecode(I('kw'))) {
            $where .= " and (A.authcode like '%$this->kw%' or  G.title like '%$this->kw%' or A.title like '%$this->kw%')";
        }
        $count = $model->where($where)->alias("A")->join("addon_auth_group as G on G.id=A.group_id")->count();
        $this->setPager($count, 8);
        $data = $model->where($where)->alias("A")->join("addon_auth_group as G on G.id=A.group_id")->order($order)->limit($this->limit)->field("A.*,G.title as group_name")->select();
        $this->data = $data;
        $this->admin_display();
    }

    /**
     * 添加权限
     */
    public function rule_add()
    {
        if ($id = I('id')) {
            $this->data = BoxModel('addon_auth')->find($id);
        }
        $this->group = M('addon_auth_group')->order('id asc')->select();
        $this->admin_display();
    }

    /**
     * 删除
     * @return [type] [description]
     */
    public function rule_del()
    {
        if ($id = I('id')) {
            BoxModel('addon_auth')->delete($id);
            $this->jump("删除成功");
        }
    }

    public function rule_add_do()
    {
        if (IS_POST) {
            $data = array(
                'group_id' => I('group_id'),
                'title' => I('title'),
                'authcode' => I('authcode'),
                'is_show' => I('is_show'),
                'short_name' => I('short_name'),
                'url' => stripslashes(htmlspecialchars_decode(I('url'))), //防止标签转义I('url');,

            );
            $model = BoxModel('addon_auth');
            if ($id = I('id')) {
                $model->where(array(
                    'id' => $id
                ))->save($data);
            } else {
                $model->add($data);
            }
            $this->jump("操作成功");
        }
    }

    /**
     * 权限组
     */
    public function group()
    {
        $where = '1=1 and is_show=1';
        $order = 'cate,order_by asc';
        $model = M('addon_auth_group');
        $data = $model->where($where)->order($order)->select();
        $rule = BoxModel('addon_auth');
        foreach ($data as $key => $value) {
            $rules = $rule->where(array(
                'id' => $value['rid']
            ))->find();
            $data[$key]['authcode'] = $rules['authcode'];
        }
        $this->data = $data;
        $this->admin_display();
    }

    /**
     * 添加组
     */
    public function group_add()
    {
        if ($id = I('id')) {
            $data = M('addon_auth_group')->where(array(
                'rid' => $id
            ))->find();
            $rule = BoxModel('addon_auth')->where(array(
                'id' => $data['rid']
            ))->find();
            $data['authcode'] = $rule['authcode'];
            $this->data = $data;
        }
        $this->admin_display();
    }

    public function group_add_do()
    {
        if (IS_POST) {
            $data = array(
                'title' => I('title'),
                'authcode' => I('authcode'),
            );
            /*$info=$this->_upload("/admin/");
            if($info){
            $data2['ico'] = $info['picname']['savepath'].$info['picname']['savename'];
            }*/
            $data2['title'] = I('title');
            $data2['ico'] = I('ico');
            $data2['cate'] = I('cate');
            $data2['url'] = stripslashes(htmlspecialchars_decode(I('url'))); //防止标签转义I('url');
            if ($id = I('id')) {
                BoxModel('addon_auth')->where(array(
                    'id' => $id
                ))->save($data);
                BoxModel('addon_auth_group')->where(array(
                    'rid' => $id
                ))->save($data2);
            } else {
                $add = BoxModel('addon_auth')->add($data);
                $data2['rid'] = $add;
                BoxModel('addon_auth_group')->add($data2);
            }
            $this->jump("操作成功");
        }
    }

    public function group_del()
    {
        if ($id = I('group_id')) {
            //   BoxModel('addon_auth')->where(array('rid'=>$id))->delete();
            BoxModel('addon_auth_group')->where(array(
                'id' => $id
            ))->delete();
            BoxModel('addon_auth')->where(array(
                'group_id' => I('group_id')
            ))->delete();
            $this->jump("操作成功");
        }
    }

    /**
     * 当前路由下的菜单
     * @return
     */
    public function getMemu($param)
    {
        $group_id = $param['auth_group_id'];
        $map = "group_id='$group_id' and is_show=1";
        /******用户**/
        if (session('admin_id') && (!session('isSuper'))) {
            $r3 = BoxModel("addon_auth_access")->where(array(
                'admin_id' => session('admin_id')
            ))->find();
            $map .= " and id in (" . $r3['access'] . ")";
        }
        $group_data = BoxModel("addon_auth")->where($map)->select();
        foreach ($group_data as $k => $one) {
            $group_data[$k]['lable'] = $one['title'];
            $group_data[$k]['key'] = $one['short_name'] ? $one['short_name'] : $one['authcode'];
            if ($one['url']) {
                $group_data[$k]['url'] = parent::fetch("", $one['url']);
                $group_data[$k]['url'] .= "?auth_id=" . $one['id'] . "&auth_group_id=" . $one['group_id'];
            }
        }
        return $group_data;
    }
}

